package swingExtensions.component;

import io.github.jonestimd.swing.component.BeanListComboBox;
import io.github.jonestimd.swing.component.BeanListComboBoxModel;
import net.miginfocom.swing.MigLayout;
import org.fluttercode.datafactory.impl.DataFactory;
import swingExtensions.component.format.UpperCaseFormat;
import swingExtensions.demo.FrameDemo;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class BeanListComboBoxDemo {

    private BeanListComboBox<String> nameComboBox;
    private BeanListComboBox<String> addressComboBox;
    private BeanListComboBoxModel<String> addressComboBoxModel;

    public JPanel createContentPanel() {
        DataFactory factory = new DataFactory();
        List<String> nameList = new ArrayList<>();
        List<String> addressList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            nameList.add(factory.getName());
            addressList.add(factory.getAddress());
        }
        nameComboBox = BeanListComboBox.builder(new UpperCaseFormat(), nameList)
                .optional()
                .editable(s -> s != null && s.length() > 10 ? null : "unmatched value")
                .get();
        addressComboBoxModel = new BeanListComboBoxModel<>();
        addressComboBox = BeanListComboBox.builder(new UpperCaseFormat(), addressComboBoxModel).get();

        JPanel panel = new JPanel(new MigLayout("fill", "[]rel[grow,fill]", ""));
        panel.add(new JLabel("选择名称"));
        panel.add(nameComboBox);
        panel.add(new JLabel("选择地址"));
        panel.add(addressComboBox);

        new Timer(10000, e -> {
            addressComboBoxModel.setElements(addressList);
        }).start();
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            BeanListComboBoxDemo demo = new BeanListComboBoxDemo();
            FrameDemo frameDemo = new FrameDemo(BeanListComboBoxDemo.class.toString());
            frameDemo.setContentPane(demo.createContentPanel());
            frameDemo.setVisible(true);
        });
    }
}
